package cc.plumgo.ss.ui.page;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.game.GameCanvas;

import cc.plumgo.j2me.utils.CustomPage;
import cc.plumgo.ss.core.Constants;
import cc.plumgo.ss.core.SchoolMidlet;

public class MenuPage extends CustomPage {
	public static final int MENU_SHOW = 0;
	public static final int MENU_START = 1;
	public static final int MENU_EXIT = 2;
	public static final int MENU_HELP = 3;
	public static final int MENU_OPTION = 4;
	public static final int MENU_ABOUT = 5;
	public static final int MENU_STATE_NUM = 6;

	public static final int RET_NEW = 0;
	public static final int RET_GOON = 1;
	
	private ShowPage about;
	private OptionPage option;
	private StartPage start;
	private ShowPage help;
	
	public MenuPage() {
		super();
	}

	public void process(int key, Graphics g) {
		loadSprite(Constants.UI_IMG_MENU, Constants.UI_MENU_FRM_SIZ[0],
				Constants.UI_MENU_FRM_SIZ[1]);

		if (selected == MENU_SHOW || selected == -1) {
			if (selected == state && selected != MENU_SHOW) {
				state = (key & GameCanvas.FIRE_PRESSED) != 0 ? MENU_SHOW
						: state;
			} else {
				if ((key & GameCanvas.UP_PRESSED) != 0) {
					state = (--state <= 0) ? (MENU_STATE_NUM - 1) : state;
				}
				if ((key & GameCanvas.DOWN_PRESSED) != 0) {
					state = (++state >= MENU_STATE_NUM) ? MENU_SHOW : state;
				}
				if ((key & GameCanvas.FIRE_PRESSED) != 0) {
					selected = state;
				}
			}

			sprite.setFrame(state);
			paint(g);
		} else {
			switch (selected) {
			case MENU_ABOUT: {
				aboutProcess(key, g);
				break;
			}
			case MENU_EXIT: {
				SchoolMidlet.midlet.notifyDestroyed();
				break;
			}
			case MENU_HELP: {
				helpProcess(key, g);
				break;
			}
			case MENU_OPTION: {
				optionProcess(key, g);
				break;
			}
			case MENU_START: {
				startProcess(key, g);
				break;
			}
			case MENU_SHOW: {
				break;
			}
			}
		}

	}

	private void aboutProcess(int key, Graphics g) {
		if (about == null) {
			about = new ShowPage(Constants.UI_IMG_ABOUT, 141, 222);
			about.setState(ShowPage.SHOW_SHOW);
		}
		about.process(key, g);
		if (about.getState() == ShowPage.SHOW_HIDE) {
			initState();
			about = null;
		}
	}

	private void helpProcess(int key, Graphics g) {
		if (help == null) {
			help = new ShowPage(Constants.UI_IMG_HELP, 159, 224);
			help.setState(ShowPage.SHOW_SHOW);
		}

		help.process(key, g);
		if (help.getState() == ShowPage.SHOW_HIDE) {
			initState();
			help = null;
		}
	}

	private void optionProcess(int key, Graphics g) {
		if (option == null) {
			option = new OptionPage(Constants.UI_IMG_OPT, 152, 224);
			option.setSprite(Constants.UI_IMG_VOL, Constants.UI_VOL_SIZ[0],
					Constants.UI_VOL_SIZ[1]);
			option.setState(OptionPage.SHOW_SHOW);
		}
		option.process(key, g);
		if (option.getState() == ShowPage.SHOW_HIDE) {
			initState();
			option = null;
		}
	}

	private void startProcess(int key, Graphics g) {
		if (start == null) {
			start = new StartPage(Constants.UI_IMG_START, 158, 224);
			start.setSprite(Constants.UI_IMG_FLW, Constants.UI_FLW_SIZ[0], Constants.UI_FLW_SIZ[1]);
			start.setState(StartPage.SHOW_SHOW);
		}
		start.process(key, g);
		if (start.getState() == ShowPage.SHOW_HIDE) {
			initState();
			start = null;
		}
	}

	private void initState() {
		state = MENU_SHOW;
		selected = -1;
	}
}
